package pro.jiug.kExcel.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import pro.jiug.kExcel.entity.User;
import pro.jiug.kExcel.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户操作
 *
 * @author Keffett
 * @since 2021-02-19
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    //首页
    @RequestMapping("index")
    public ModelAndView queryPage(HttpServletRequest request, HttpServletResponse response) {
        //查询结果
        User user = userService.getById(1);

        //封装返回值
        ModelAndView mav=new ModelAndView();
        mav.addObject("user",user);
        mav.setViewName("index");
        return mav;
    }

    //查询
    @RequestMapping("queryPage")
    public JSONObject queryPage(HttpServletRequest request, HttpServletResponse response,
                                @RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,
                                User user) {
        //查询结果
        PageInfo<User> page = userService.findPage(pageNum,pageSize, user);

        //封装返回值
        JSONObject resJSON=new JSONObject();
        resJSON.put("page",page);
        return resJSON;
    }
    //查询同名用户
    @RequestMapping("querySameNameUser")
    public JSONObject querySameNameUser(HttpServletRequest request, HttpServletResponse response,
                                User user) {

        //查询结果
        List<User> users = userService.findSameNameUser(user);
        //user.setId(1).setName("nihao").setPassword("5");
        //封装返回值
        JSONObject resJSON=new JSONObject();
        resJSON.put("users",users);
        return resJSON;
    }

    //新增
    @RequestMapping("add")
    public JSONObject add(HttpServletRequest request, HttpServletResponse response,
                          User user) {
        //封装返回值
        JSONObject resJSON=new JSONObject();
        try{
            userService.save(user);
            resJSON.put("code",1);
            resJSON.put("userid",user.getId());
        } catch (Exception e) {
            resJSON.put("code",0);
            e.printStackTrace();
        }
        return resJSON;
    }
    //编辑
    @RequestMapping("edit")
    public JSONObject edit(HttpServletRequest request, HttpServletResponse response,
                          User user) {
        //封装返回值
        JSONObject resJSON=new JSONObject();
        try{
            boolean success = userService.updateById(user);
            resJSON.put("code",success?1:0);
        } catch (Exception e) {
            resJSON.put("code",0);
            e.printStackTrace();
        }
        return resJSON;
    }

    //删除
    @RequestMapping("delete")
    public JSONObject delete(HttpServletRequest request, HttpServletResponse response,
                           Integer userId) {
        //封装返回值
        JSONObject resJSON=new JSONObject();
        try{
            boolean success = userService.removeById(userId);
            resJSON.put("code",success?1:0);
        } catch (Exception e) {
            resJSON.put("code",0);
            e.printStackTrace();
        }
        return resJSON;
    }

}

